package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.client.UserBankService;
import com.fqgj.xjd.user.client.enums.UserBindCardTypeEnum;
import com.fqgj.xjd.user.client.enums.UserCardStatusEnum;
import com.fqgj.xjd.user.client.request.UserBank;
import com.fqgj.xjd.user.client.response.UserBankInfo;
import com.fqgj.xjd.user.common.enums.BindCardValidStatusEnum;
import com.fqgj.xjd.user.config.ConfigUtil;
import com.fqgj.xjd.user.dao.UserAccessDao;
import com.fqgj.xjd.user.dao.UserAuthBankDao;
import com.fqgj.xjd.user.dao.UserDao;
import com.fqgj.xjd.user.entity.UserAuthBankEntity;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/10
 * Time: 下午3:36
 */
@Service("userBankService")
public class UserBankServiceImpl implements UserBankService {

    @Autowired
    ConfigUtil apolloConfigUtil;
    @Autowired
    UserAuthBankDao userAuthBankDao;
    @Autowired
    UserDao userDao;
    @Autowired
    UserAccessDao userAccessDao;

    /**
     * @param userBank
     * @return
     */
    @Override
    public Response<Boolean> bind(UserBank userBank) {
        UserAuthBankEntity userAuthBankEntity = userAuthBankDao.selectUserBankByUserCode(userBank.getUserCode());
        if (userAuthBankEntity == null) {
            userAuthBankEntity = new UserAuthBankEntity();
            if (null != userBank.getUserCode()) {
                userAuthBankEntity.setUserCode(userBank.getUserCode());
            }
            if (StringUtils.isNotEmpty(userBank.getCode())) {
                userAuthBankEntity.setCode(userBank.getCode());
            }
            if (StringUtils.isNotEmpty(userBank.getName())) {
                userAuthBankEntity.setName(userBank.getName());
            }
            if (null != userBank.getBindSuccess()) {
                userAuthBankEntity.setStatus(BindCardValidStatusEnum.init_status.getStatus());
            }
            if (StringUtils.isNotEmpty(userBank.getNumber())) {
                userAuthBankEntity.setCardNo(StringUtils.trimToEmpty(userBank.getNumber()));
            }
            if (null != userBank.getMobile() && StringUtils.isNotEmpty(userBank.getMobile().getMobile())) {
                userAuthBankEntity.setMobile(userBank.getMobile().getMobile());
            }
            if (null != userBank.getBankProviderEnum()) {
                userAuthBankEntity.setType(userBank.getBankProviderEnum().getCode());
            }
            //TODO zsp mock
            if (apolloConfigUtil.isMockBindCard()) {
                userAuthBankEntity.setType(UserBindCardTypeEnum.CARD_51.getCode());
                userAuthBankEntity.setStatus(UserCardStatusEnum.CHECK_SUCCESS.getStatus());
                if (StringUtils.isEmpty(userBank.getName())) {
                    userAuthBankEntity.setName("test bank");
                }
            }
            userAuthBankDao.insert(userAuthBankEntity);
        } else {
            if (StringUtils.isNotEmpty(userBank.getName())) {
                userAuthBankEntity.setName(userBank.getName());
            }
            if (StringUtils.isNotEmpty(userBank.getNumber())) {
                userAuthBankEntity.setCardNo(userBank.getNumber());
            }
            if (null != userBank.getMobile() && StringUtils.isNotEmpty(userBank.getMobile().getMobile())) {
                userAuthBankEntity.setMobile(userBank.getMobile().getMobile());
            }
            if (null != userBank.getBindSuccess()) {
                userAuthBankEntity.setStatus(userBank.getBindSuccess() ? BindCardValidStatusEnum.valid_suc.getStatus() : BindCardValidStatusEnum.init_status.getStatus());
            }
            if (null != userBank.getUserCardStatusEnum()) {
                userAuthBankEntity.setStatus(userBank.getUserCardStatusEnum().getStatus());
            }
            if (null != userBank.getBankProviderEnum()) {
                userAuthBankEntity.setType(userBank.getBankProviderEnum().getCode());
            }
            if (null != userBank.getCode()) {
                userAuthBankEntity.setCode(userBank.getCode());
            }
            userAuthBankDao.updateByUserCode(userAuthBankEntity);
        }
        return Response.ok().putData(true);
    }

    /**
     * 绑卡成功
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<Boolean> setCardBinded(String userCode) {
        return Response.error("Interface failure");
    }

    /**
     * 重绑卡状态
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<Boolean> setCardRebind(String userCode) {
        return Response.error("Interface failure");
    }

    /**
     * @param userCode
     * @return
     */
    @Override
    public Response<UserBankInfo> getByUserCode(String userCode) {

        UserAuthBankEntity userAuthBankEntity = userAuthBankDao.selectUserBankByUserCode(userCode);

        if (userAuthBankEntity == null) {
            return Response.ok().putData(null);
        }

        UserBankInfo userBankInfo = new UserBankInfo();

        BeanUtils.copyProperties(userAuthBankEntity, userBankInfo);

        return Response.ok().putData(userBankInfo);
    }

    @Override
    public Response<List<UserBankInfo>> getBankListByUserCode(String userCode) {

        List<UserAuthBankEntity> bankEntities = userAuthBankDao.selectAllUserBankByUserCode(userCode);
        List<UserBankInfo> userBankInfos = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(bankEntities)){
            bankEntities.forEach(s->{
                UserBankInfo userBankInfo = new UserBankInfo();
                BeanUtils.copyProperties(s, userBankInfo);
                userBankInfos.add(userBankInfo);
            });
        }
        return Response.ok().putData(userBankInfos);
    }

    /**
     * @param userCodes
     * @return
     */
    @Override
    public Response<List<UserBankInfo>> getListByUserCodes(List<String> userCodes) {
        List<UserAuthBankEntity> userAuthBankEntityList = userAuthBankDao.selectUserBankByUserCodes(userCodes);

        List<UserBankInfo> userBankInfoList = new ArrayList<UserBankInfo>();

        for (UserAuthBankEntity userAuthBankEntity : userAuthBankEntityList) {
            UserBankInfo userBankInfo = new UserBankInfo();

            BeanUtils.copyProperties(userAuthBankEntity, userBankInfo);
            userBankInfoList.add(userBankInfo);
        }
        return Response.ok().putData(userBankInfoList);
    }

    /**
     * @param
     */
    @Override
    public void setDeleted(String userCode) {
        UserAuthBankEntity userAuthBankEntity = userAuthBankDao.selectUserBankByUserCode(userCode);
        userAuthBankDao.deleteByPrimaryKey(userAuthBankEntity.getId());
    }
}
